<?php

namespace App\Http\Controllers;


use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use function Symfony\Component\String\u;

//use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(){
        return view('admin.login');
    }
    public function doLogin(Request $request){
        $name=$request->input('name');
        $password=$request->input('password');
        if(Auth::attempt(['name'=>$name,'password'=>$password]))
        {
            return json_encode(['success'=>true,'msg'=>'用户登陆成功！']);
        }
        else{
            return json_encode(['success'=>false,'msg'=>'用户名或密码错误,请重新登录']);
        }


    }
    public function logout(){
        Auth::logout();
        return redirect('/adm/login');
    }
    public function index()
    {
        $oUsers = User::paginate(8);
        return view('admin.admin-user')->with('oUsers',$oUsers);
    }
    public function expert()//后台专家管理模块
    {
        return view('admin.admin-expert');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.admin-adduser');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return false|\Illuminate\Http\Response|string
     */
    public function store(Request $request)//增加管理员用户功能，已实现
    {
        $data = $request->input();
        $rules = [
            'name'=>'required|min:3|max:30|alpha_num|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $message = [
            'name.required'=>'用户姓名不能为空，必须填写',
            'name.alpha'=>'用户名有字母或数字组成',
            'name.min'=>'用户名长度为3-30个字符',
            'name.max'=>'用户名长度为3-30个字符',
            'name.unique'=>'用户名已注册',
            'password.confirmed'=>'密码不一致',
            'password.required'=>'请填写密码',
            'password.min'=>'密码长度大于6个字符',
        ];
        $validate = Validator::make($data,$rules, $message);
        if($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{
            $data['password'] = Hash::make($data['password']);
            User::create($data);
            return json_encode(['success'=>true,'msg'=>'成功提交']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function edit($id){//修改密码后台页面，已实现
        $user = User::find($id);
        if($user ==NULL){
            abort(404);
        }
        return view('admin.admin-edituser')->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id,Request $request){//修改密码功能,已实现
        $user = User::find($id);
        if($user ==NULL){
            abort(404);
        }
        $datas = $request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码太短，至少要有:min个字符',
            'password.confirmed'=>'两次密码输入不一致',
        ];
        $validate = Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user->password =Hash::make($datas['password']);
        $user->save();
        return json_encode(['success'=>true,'msg'=>'success']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
     */
    public function destroy(Request $request)//删除管理员功能,不能删除自己功能,已实现
    {
        $data = $request->input();
//        var_dump($data['id']);
//        return var_dump();

        $user = User::find($data['id']);
        $user->delete();
        return redirect('/adm/user');
    }
}
